<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item label='类型' name='type' :rules="[{required : true}]">
            <a-radio-group v-model:value="formData.type" name="type">
                <a-radio :value="1">Android</a-radio>
                <a-radio :value="2">IOS</a-radio>
            </a-radio-group>
        </a-form-item>
        <a-form-item label='名称' name='name' :rules="[{required : true, message : '请填写名称'}]">
            <a-input type='text' v-model:value='formData.name' />
        </a-form-item>
        <a-form-item label='版本号' name="version" :rules="[{required : true, message : '请填写版本号'}]">
            <a-input type='text' v-model:value='formData.version' />
        </a-form-item>
        <a-form-item label='版本序号' name="vno" :rules="[{required : true, message : '请填写版本序号'}]">
            <a-input-number type='number' v-model:value.number='formData.vno' />
        </a-form-item>
        <!-- <a-form-item label='Android 版本号' :name="['android', 'version']" :rules="[{required : true, message : '请填写版本号'}]">
            <a-input type='text' v-model:value='formData.android.version' />
        </a-form-item> -->
        <a-form-item v-if="formData.type == 1" label='Android安装包' name="file">
            <a-upload :max-count="1" v-model:file-list='coverList' :beforeUpload='beforeUpload' :customRequest='uploadAndroid'>
                <template v-if='coverList.length == 0'>
                    <a-button style="width:150px"><UploadOutlined />选择安装包</a-button>
                </template>
            </a-upload>
        </a-form-item>
        <a-form-item label='链接' name="url">
            <a-input type='text' v-model:value='formData.url' />
        </a-form-item>
        <!-- <a-form-item label='IOS版本号' :name="['ios', 'version']" :rules="[{required : true, message : '请填写版本号'}]">
            <a-input type='text' v-model:value='formData.ios.version' />
        </a-form-item>
        <a-form-item label='IOS链接' :name="['ios', 'url']" :rules="[{required : true, message : '请填写版本号'}]">
            <a-input type='text' v-model:value='formData.ios.url' />
        </a-form-item> -->
        <a-form-item label='标题' name='title'>
            <BLangSelect v-model='formData.title' />
        </a-form-item>
        <a-form-item label='内容' name='description'>
            <BLangSelect v-model='formData.description' />
        </a-form-item>
        <a-form-item label='强制更新' name='forceUpdate'>
            <a-switch v-model:checked='formData.forceUpdate' checked-children='是' un-checked-children='否'/>
        </a-form-item>
    </a-form>
</template>

<script lang="ts" setup>
import { localeOptionsApi } from "@/api/system/Locale";
    import { saveApi, uploadApi } from '@/api/AppVersion';
        import { UploadOutlined } from '@ant-design/icons-vue';
        import { ref } from 'vue';


    const props = defineProps<{
        row? : any;
        category? : number
    }>()

    const coverList = ref<any[]>([])

    const locales = ref<any[]>([])

    const formRef = ref()

    
    const formData = ref<any>({
        id : 0,
        name : '',
        groupId : '',
        title : '',
        description : '',
        type : 1,
        version : '',
        vno : '',
        url : '',
        file : '',
        // android : {
        //     version : '',
        //     file : ''
        // },

        // ios : {
        //     version : '',
        //     url : ''
        // },


        forceUpdate : true

    })

    if(props.row) {
        for(let k in props.row) {
            if(k in formData.value) {
                formData.value[k] = props.row[k]
            }
        }

        if(props.row.file) {
            coverList.value[0] = {
                name : import.meta.env.B_NET_STATIC_URL + props.row.file,
                status : 'done',
                url : import.meta.env.B_NET_STATIC_URL + props.row.file
            }
        }

    }

    async function submit() {
        let params  = await formRef.value.validate()
        if('sort' in params && params.sort === '') {
            params.sort = 0
        }


        return saveApi(params)
    }


    function uploadAndroid(e) {
        uploadApi(e.file).then(res => {
            coverList.value[0].status = 'done'
            coverList.value[0].url = import.meta.env.B_NET_STATIC_URL + res.data
            formData.value.file = res.data

            formRef.value.validateFields(['file'])
        })
    }

    function beforeUpload(file:File, field:FormField) {
        return true
    }

    localeOptionsApi().then(data => {
        locales.value = data
    })


    defineExpose({
        submit
    })
</script>